package com.ruoyi.system.util;


import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.locationtech.jts.geom.*;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 解析shp文件
 * @author xujiajia
 *
 */
public class WdcShpUtil {

    public  static  Map<String,String> shipZb(File file){
        ShapefileDataStore shpDataStore = null;
        //shp文件所在路径，要保证shp、dbf、shx都在同一个文件夹中
//        File file = new File("D:\\shpdemo042701\\contourPolygon1.shp");
        try {
            // 1. 打开数据存储（需手动关闭）
            shpDataStore = new ShapefileDataStore(file.toURI().toURL());
            shpDataStore.setCharset(StandardCharsets.UTF_8);

            String typeName = shpDataStore.getTypeNames()[0];
            SimpleFeatureSource featureSource = shpDataStore.getFeatureSource(typeName);

            try (SimpleFeatureIterator iterator = featureSource.getFeatures().features()) {
                while (iterator.hasNext()) {
                    SimpleFeature feature = iterator.next();
                    Geometry geometry = (Geometry) feature.getDefaultGeometry();// 直接获取默认几何属性
                    Map<String,String> ff =   processGeometry(geometry); // 统一处理几何结构
                    if (ff!=null){
                        return ff;
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 3. 手动释放 ShapefileDataStore
            if (shpDataStore != null) {
                shpDataStore.dispose();
            }
        }
        return null;
    }

    private static  Map<String,String> processGeometry(Geometry geometry) {
        if (geometry instanceof MultiPolygon) {
            // 处理MultiPolygon中的每个Polygon  也就是处理多边形的集合
            MultiPolygon multiPolygon = (MultiPolygon) geometry;
            for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
                Geometry geometryN = multiPolygon.getGeometryN(i);

                // 递归处理每个子多边形
//                processGeometry(geometryN);
                // 打印子几何对象的中心点
                Map<String,String> g =  printCentroid(geometryN);
                if (g!=null){
                    return g;
                }
            }
        }
//        else if (geometry instanceof Polygon) {
//            // 处理单个多边形
//            Polygon polygon = (Polygon) geometry;
//            // 处理外环
//            processLineString(polygon.getExteriorRing());
//            // 处理所有内环（孔洞）
//            for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
//                processLineString(polygon.getInteriorRingN(i));
//            }
//        }

        return null;
    }

    private static Map<String,String> printCentroid(Geometry geometry) {

        if (geometry == null) {
            return null;
        }
        Map<String, String> stringStringHashMap = new HashMap<>();
        // 获取几何对象的质心（中心点）
        Point centroid = geometry.getCentroid();
        if (centroid != null) {
            Coordinate centerCoord = centroid.getCoordinate();
            System.out.printf("中心点坐标: (%f,%f)%n", centerCoord.x, centerCoord.y);
            stringStringHashMap.put("x",String.valueOf(centerCoord.x));
            stringStringHashMap.put("y",String.valueOf(centerCoord.y));
            return  stringStringHashMap;
        }
        return null;
    }

    private static void processLineString(LineString line) {
        //提取坐标点
        for (Coordinate coord : line.getCoordinates()) {
            System.out.printf("(%f,%f)%n", coord.x, coord.y);
        }
    }




}